﻿using System;
using System.Collections.Generic;
using System.Text;
using MeTalker.Model;
using MeTalker.DAL;
using System.Data;

namespace MeTalker.BLL
{
    public class MembersBLL
    {
        public static int Update(MembersMod members) {
            return MembersDAL.Update(members);
        }
        public static int Delete(string autoId)
        {
            return MembersDAL.Delete(autoId);
        }
        public static int Delete(int autoId)
        {
            return MembersDAL.Delete(autoId);
        }

        public static void Heart(string sessionId, string userName)
        {
            MembersDAL.Heart(sessionId, userName);

        }

        public static MembersMod CheckSoftHD(MembersMod member)
        {
            try
            {
                DataTable dt = MembersDAL.CheckSoftHD(member);
                if (dt.Rows.Count <= 0)
                {
                    return null;
                }
                List<MembersMod> members = new List<MembersMod>();
                foreach (DataRow row in dt.Rows)
                {
                    members.Add(SetModel(row));
                }
                if (members.Count <= 0)
                    return null;
                return members[0];
            }
            catch { return null; }
            //try
            //{
            //    DataTable dt = MembersDAL.CheckSoftHD(members);
            //    if (dt != null && dt.Rows.Count > 0)
            //        return true;
            //    return false;
            //}
            //catch { return false; }
        }
        public static DataTable GetUsers(MembersMod members)
        {
            return MembersDAL.GetUsers(members);
        }
        
        public static DataTable GetFilter(string filter)
        {
            return MembersDAL.GetFilter(filter);
        }

        public static List<MembersMod> GetFilterToList(string filter)
        {
            DataTable dt = MembersDAL.GetFilter(filter);
            List<MembersMod> mods = new List<MembersMod>();
            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    mods.Add(SetModel(row));
                }
            }
            return mods;
        }

        public static MembersMod GetMembers(int autoID)
        {
            DataTable dt = MembersDAL.GetMembers(autoID);
            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            foreach (DataRow row in dt.Rows)
            {
                return SetModel(row);
            }
            return null;
        }

        public static DataTable GetUsers()
        {
            return MembersDAL.GetUsers();
        }

        public static int Insert(MembersMod members)
        {
            return MembersDAL.Insert(members);
        }

        static MembersMod SetModel(DataRow row)
        {
            MembersMod member = new MembersMod();
            member.AutoID = int.Parse(row["AutoID"].ToString());
            member.AddDate = DateTime.Parse(row["AddDate"].ToString());
            member.Email = row["Email"].ToString();
            member.ExpireDate = DateTime.Parse(row["ExpireDate"].ToString());
            member.HDID = row["HDID"].ToString();
            member.IsValid = bool.Parse(row["IsValid"].ToString());
            try
            {
                member.LatestOnline = DateTime.Parse(row["LatestOnline"].ToString());
            }
            catch { }
            //member.LicenseDate = DateTime.Parse(row["LicenseDate"].ToString());
            member.LoginFlag = bool.Parse(row["LoginFlag"].ToString());
            member.Memo = row["Memo"].ToString();
            member.MobilePhone = row["MobilePhone"].ToString();
            member.QQ = row["QQ"].ToString();
            member.Password = row["Password"].ToString();
            member.RegistDate = DateTime.Parse(row["RegistDate"].ToString());
            member.SessionId = row["SessionId"].ToString();
            member.SoftID = row["SoftID"].ToString();
            member.SoftName = row["SoftName"].ToString();
            member.UserName = row["UserName"].ToString();
            member.UserType = row["UserType"].ToString();
            //member.IsActivate = bool.Parse(row["IsActivate"].ToString());
            return member;
        }

        public static void Logout(string uid, string sessionId)
        {
            MembersDAL.Logout(uid, sessionId);
        }

        public static MembersMod SignUser(MembersMod members)
        {
            return SignUser(members.UserName, members.Password,members.SoftID);
        }

        public static MembersMod SignUser(string userName, string pwd,string soft)
        {
            try
            {
                DataTable dt = MembersDAL.SignUser(userName, pwd, soft);
                if (dt.Rows.Count <= 0)
                {
                    return null;
                }
                List<MembersMod> members = new List<MembersMod>();
                foreach (DataRow row in dt.Rows)
                {
                    members.Add(SetModel(row));
                }
                if (members.Count <= 0)
                    return null;
                return members[0];
            }
            catch
            {
                return null;
            }
        }

        public static void UpdateLoginState(MembersMod members)
        {
            UpdateLoginState(members, true);
        }
        public static void UpdateLoginState(MembersMod members, bool loginFalg)
        {
            members.LoginFlag = loginFalg;
            MembersDAL.UpdateLoginState(members);
        }

        //public static void UpdateActivate(MembersMod members)
        //{
        //    UpdateActivate(members, true);
        //}

        //public static void UpdateActivate(MembersMod members, bool isActivate)
        //{
        //    members.IsActivate = isActivate;
        //    MembersDAL.Update(members);
        //}

        public static void UpdateValid(MembersMod members)
        {
            UpdateValid(members, true);
        }
        public static void UpdateValid(MembersMod members, bool isValid)
        {
            members.IsValid = isValid;
            MembersDAL.Update(members);
        }

        public static bool CheckEmail(string email)
        {
            return MembersDAL.CheckEmail(email);
        }
        public static bool CheckUserName(string userName, out int errcode)
        {
            return MembersDAL.CheckUserName(userName, out  errcode);
        }
        public static bool CheckUserName(string userName, string softId, out int errcode)
        {

            return MembersDAL.CheckUserName(userName, softId, out  errcode);
        }
        public static bool CheckHDID(string hdid)
        {
            return MembersDAL.CheckHDID(hdid);
        }
    }
}